<template>
	<view>
		<scan-layout ref="scanLayout" :tabsList="tabsList" sum-confirm-text="确认出库" @add-click="addClick" @product-change="productChange" @confirm="confirmClick">
			<u-form :model="form" ref="form" label-width="180" slot="ext-form">
				<u-form-item label="出库类型" prop="outType">
					<u-input v-model="showForm.outType" type="select" @click="outStoragePickerVisible = true"></u-input>
					<u-select v-model="outStoragePickerVisible" :list="outStorageTypeDict" :default-value="outStorageTypeDefValue" @confirm="outStoragePickerConfirm"></u-select>
				</u-form-item>
				<u-form-item label="上样门店" prop="shop"><u-input v-model="form.shop" :disabled="form.outType == '1'" @blur="shopBlur" /></u-form-item>
			</u-form>
		</scan-layout>
	</view>
</template>

<script>
import { mapActions, mapMutations } from 'vuex';
import ScanLayout from './components/scanLayout/scanLayout.vue';
import { _outStorageTypeDict } from '../../utils/dict.js';

let outStorageTypeDict = Object.keys(_outStorageTypeDict).map(key => {
	return {
		label: _outStorageTypeDict[key],
		value: key
	};
});
export default {
	components: {
		ScanLayout
	},
	data() {
		return {
			tabsList: [
				{
					name: '出库扫描'
				},
				{
					name: '数据汇总'
				}
			],
			rules: {
				shop: [
					{
						validator: (rule, value, callback) => {
							if (this.form.outType == '0' && value == '') {
								callback('请输入上样门店');
							} else {
								callback();
							}
						}
					}
				]
			},
			form: {
				outType: '1',
				shop: ''
			},
			showForm: {
				outType: _outStorageTypeDict['1']
			},
			outStorageTypeDict: outStorageTypeDict,
			outStoragePickerVisible: false,
			outStorageTypeDefValue: ['1']
		};
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
	},
	beforeDestroy() {
		this.resetScanDeviceInfo();
		this.resetSumList();
	},
	methods: {
		...mapActions({
			outStorageDeviceOther: 'storageOrder/outStorageDeviceOther',
			getDeviceByCustomer: 'storageOrder/getDeviceByCustomer'
		}),
		...mapMutations({
			resetSumList: 'storageOrder/resetSumList',
			dropSumListItem: 'storageOrder/dropSumListItem',
			setSumList: 'storageOrder/setSumList',
			resetScanDeviceInfo: 'storageOrder/resetScanDeviceInfo',
			updateScanDeviceInfo: 'storageOrder/updateScanDeviceInfo',
			updateCanAdd: 'storageOrder/updateCanAdd'
		}),
		outStoragePickerConfirm(e) {
			console.log(e);
			this.form.outType = e[0].value;
			this.showForm.outType = e[0].label;
			if (e[0].value == '0') {
				this.form.shop = '';
			}
			this.updateScanDeviceInfo({
				outType: this.form.outType
			});
		},
		shopBlur(e) {
			this.updateScanDeviceInfo({
				shop: this.form.shop
			});
		},
		productChange(e) {
			if (e.isValid) {
				this.getDeviceByCustomer({
					code: this.$store.state.storageOrder.scanDeviceInfo.code
				})
					.then(res => {
						console.log(res);
						if (res && res.code == 0 && res.data) {
							if (res.data.ckZt == 1 || res.data.xsZt == 1) {
								this.updateCanAdd(false);
								uni.showToast({
									title: '该机身码已出货',
									icon: 'none'
								});
								return;
							}
							if (res.data.rkZt) {
								this.$refs.form.validate(valid => {
									if (valid) {
										console.log('pageForm', valid);
										this.updateCanAdd(true);
									} else {
										this.updateCanAdd(false);
									}
								});
							} else {
								this.updateCanAdd(false);
								uni.showToast({
									title: '机身码尚未入库',
									icon: 'none'
								});
							}
						} else {
							this.updateCanAdd(false);
							uni.showToast({
								title: '机身码尚未入库',
								icon: 'none'
							});
						}
					})
					.catch(err => {
						console.log(err);
						this.updateCanAdd(false);
						uni.showToast({
							title: '网络错误，查询机身码失败',
							icon: 'none'
						});
					});
			} else {
				this.updateCanAdd(false);
			}
		},
		addClick() {
			this.$refs.form.validate(valid => {
				if (valid) {
					this.$refs.scanLayout.addToList();
					this.updateScanDeviceInfo(this.form);
				}
			});
		},
		confirmClick() {
			this.$confirm('提示', '确定出库？', this.outStorage);
		},
		async outStorage() {
			uni.showLoading({
				mask: true,
				title: '请求中'
			});
			let list = this.$store.state.storageOrder.sumList;
			let successCodeArr = [];
			let failCodeArr = [];
			for (let i = 0; i < list.length; i++) {
				let data = list[i];
				if (data.checked) {
					try {
						let res = await this.outStorageDeviceOther({
							...data
						});
						if (res && res.code == 0) {
							successCodeArr.push(data.code);
						} else {
							failCodeArr.push(data.code);
						}
					} catch (e) {
						failCodeArr.push(data.code);
						continue;
					}
				}
			}
			uni.hideLoading();
			if (failCodeArr.length > 0) {
				let newList = [];
				list.forEach(e => {
					if (successCodeArr.indexOf(e.code) == -1) {
						newList.push(e);
					}
				});
				this.setSumList(newList);
				uni.showModal({
					title: `有${failCodeArr.length}条数据请求失败，请重新尝试`,
					showCancel: false
				});
			} else {
				this.dropSumListItem();
				uni.showToast({
					icon: 'success',
					title: '提交成功'
				});
			}
		}
	}
};
</script>

<style lang="less">
@import './styles/table.less';
</style>
